/*
 *  Copyright (C) 2011 glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.builders;

import org.balisunrise.annotation.util.NameUtil;
import org.balisunrise.builder.BuilderException;
import org.balisunrise.common.EntityActions;
import org.balisunrise.swing.components.EntityActionAcceleratorAction;
import org.balisunrise.swing.components.panel.ActionPanel;
import org.balisunrise.swing.components.panel.layouts.EntityActionLayoutBuilder;

/**
 *
 * @author glauco
 */
public class EntityActionBuilder
        implements org.balisunrise.builder.EntityActionBuilder{

    private static final String MESSAGE = "Erro ao construir um EntityAction.\n";

    @Override
    public EntityActions create(Class entityClass, String alias)
            throws BuilderException {

        if(entityClass == null){
            throw new BuilderException(MESSAGE +
                    "entityClass não pode ser nulo.");
        }

        if(alias == null){
            throw new BuilderException(MESSAGE +
                    "alias não pode ser nulo.");
        }

        org.balisunrise.swing.commons.EntityActions ea =
                new org.balisunrise.swing.commons.EntityActions();

        ea.setAlias(alias);
        ea.setEntityClass(entityClass);
        ea.setTitle("");
        ea.setVisualRepresentation(
                new ActionPanel(new EntityActionLayoutBuilder()));

        String name = NameUtil.buildName(ea);
        ea.setName(name);
        ea.getVisualRepresentation().setName(name);
        ea.getVisualRepresentation()
                .getAccessibleContext().setAccessibleName(name);

        EntityActionAcceleratorAction a = new EntityActionAcceleratorAction(ea);

        return ea;
    }

}
